<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 控件数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\WorkTimeRecordService;
use think\Request;

class WorkTimeRecordController extends VerifyController
{
    /**
     * @var WorkTimeRecordService
     */
    private $workTimeRecordService;

    public function __construct()
    {
        parent::__construct();
        $this->workTimeRecordService = WorkTimeRecordService::getInstance();
    }

    /**
     * 获取工时记录
     * @param Request $request
     * @return \think\Response
     */
    public function getWorkTimeRecord(Request $request)
    {
        $param = $request->param();
        $tenantId = $param['param']['filter']['tenant_id'] ?? $request->getTenantId();
        $userId = $param['param']['filter']['user_id'] ?? $request->getUserId();
        $unionId = null;
        //如果不存在传如当前用户
        if (!isset($param['param']['filter']['user_id'])) {
            $unionId = $request->getUnionUserId();
        }
        $date = $param['param']['filter']['date'];
        $projectId = $param['param']['filter']['project_id'] ?? [];
        $taskFilter = $param['param']['task_filter'] ?? [];
        $res = $this->workTimeRecordService->getWorkTimeRecord($tenantId, $userId, $date, $projectId, $unionId, $taskFilter);
        return json(success_response('', $res));
    }

    /**
     * 获取任务工时记录
     * @param Request $request
     * @return \think\Response
     * @throws \think\Exception
     */
    public function getTaskWorkTimeRecord(Request $request)
    {
        $param = $request->param();
        $taskId = $param['param']['filter']['task_id'] ?? [];
        $res = $this->workTimeRecordService->getTaskWorkTimeRecord($taskId);
        return json(success_response('', $res));
    }

    /**
     * 记录工时
     * @param Request $request
     * @return \think\Response
     * @throws \think\Exception
     */
    public function recordWorkTime(Request $request)
    {
        $data = $request->param()['data'];
        try {
            $res = $this->workTimeRecordService->recordWorkTime($data);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取天的工时记录
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function getDayWorkTimeRecordList(Request $request)
    {
        $filter = $request->param()['param']['filter'];
        $page = $request->param()['param']['page'] ?? [1, C("database.database_max_select_rows")];
        $res = $this->workTimeRecordService->getDayWorkTimeRecordList($filter, $page);
        return json(success_response('', $res));
    }

    /**
     * 更新工时记录
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function updateWorkTimeRecord(Request $request)
    {
        $filter = $request->param()['data'];
        try {
            $res = $this->workTimeRecordService->updateWorkTimeRecord($filter);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取指定日期的工时
     * @param Request $request
     * @return \think\Response
     */
    public function getSpecifiedDayWorkTime(Request $request)
    {
        $filter = $request->param()['param']['filter'];
        $filter["user_id"] = $filter["user_id"] ?? $request->getUserId();
        $filter["tenant_id"] = $filter["tenant_id"] ?? $request->getTenantId();
        $res = $this->workTimeRecordService->getSpecifiedDayWorkTime($filter);
        return json(success_response('', $res));
    }

    /**
     * 工时记录任务搜索
     * @param Request $request
     * @return \think\Response
     * @throws \think\Exception
     */
    public function taskSearch(Request $request)
    {
        $filter = $request->param()['param']['filter'];
        $page = $request->param()['param']['page'] ?? [1, C("database.database_max_select_rows")];
        $res = $this->workTimeRecordService->taskSearch($filter, $page, $request->getUserId(), $request->getTenantId());
        return json(success_response('', $res));
    }

    /**
     * 任务工时切换历史记录
     * @param Request $request
     * @return \think\Response
     * @throws \think\Exception
     */
    public function optionCheckHistoryList(Request $request)
    {
        $filter = $request->param()['param']['filter'];
        $page = $request->param()['param']['page'] ?? [1, C("database.database_max_select_rows")];
        $res = $this->workTimeRecordService->optionCheckHistoryList($filter, $page, $request->getUserId(), $request->getTenantId());
        return json(success_response('', $res));
    }

    /**
     * 批量删除
     * @param Request $request
     * @return \think\Response|void
     * @throws \think\Exception
     */
    public function batchDelete(Request $request)
    {
        try {
            $filter = $request->param()['param']['filter'];
            $res = $this->workTimeRecordService->batchDelete($filter);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }


    /**
     * 批量更新工时记录
     * @param Request $request
     * @return \think\Response|void
     * @throws \think\Exception
     */
    public function batchUpdateWorkTimeRecord(Request $request)
    {
        try {
            $filter = $request->param()['param']['filter'];
            $data = $request->param()['param']['data'];
            $res = $this->workTimeRecordService->batchUpdateWorkTimeRecord($filter, $data, $request->getUserId(), $request->getTenantId());
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

}
